# -*- coding: UTF-8 -*-
from choices import *
from django.db import models
from django.contrib import admin
import datetime

# Create your models here.

class User(models.Model):
    """Definition of MicroBlog User

    Necessary columns are:
        id --> auto increamental unique
        email --> unique
        nick_name --> unique, limit 30 characters
        password --> 6-20

    Optional columns are:
        domain_suffix --> unique, Default Null, limit 30 characters
        private_settings --> several fixed choices, Default All Public
        gender --> several fixed choices, Default Secret
        birthday --> Default Null
        location --> several fixed choices, Default Unknown
        hometown --> several fixed choices, Default Unknown
        personal_site --> Url, Default Null
        personal_state --> limit 300 characters, Default Blank
        cellphone --> limit 11-number
        marital_status --> several fixed choices, Default Secret
        musics --> limit 300 characters, Default Null
        movies --> limit 300 characters, Default Null
        books --> limit 300 characters, Default Null
        sports --> limit 300 characters, Default Null
        people --> limit 300 characters, Default Null
        image --> limit 120 characters, Default Null
        fans --> manytomanyfield asymmetrical
        new_at_count --> default 0
        new_fans_count --> default 0
    """
    
    email = models.EmailField(unique = True)
    nick_name = models.CharField(unique = True, max_length = 30)
    password = models.CharField(max_length = 20)

    domain_suffix = models.CharField(unique = True, null = True, max_length = 30)
    private_settings = models.CharField(max_length = 1, choices = PRIVATE_SETTINGS_CHOICES, default = PRIVATE_SETTINGS_CHOICES[0][0])
    gender = models.CharField(max_length = 1, choices = GENDER_CHOICES, default = GENDER_CHOICES[2][0])
    birthday = models.DateField(null = True, blank = True)
    location = models.CharField(max_length = 2, choices = PROVINCE_CHOICES, default = PROVINCE_CHOICES[0][0])
    hometown = models.CharField(max_length = 2, choices = PROVINCE_CHOICES, default = PROVINCE_CHOICES[0][0])
    personal_site = models.URLField(verify_exists=False, null = True, blank = True)
    personal_state = models.CharField(max_length = 300, null = True, blank = True)
    cellphone = models.CharField(max_length = 11, null = True, blank = True)
    marital_status = models.CharField(max_length = 1, choices = MARITAL_STATUS_CHOICES, default = MARITAL_STATUS_CHOICES[0][0])
    musics = models.CharField(max_length = 300, null = True, blank = True)
    movies = models.CharField(max_length = 300, null = True, blank = True)
    books = models.CharField(max_length = 300, null = True, blank = True)
    sports = models.CharField(max_length = 300, null = True, blank = True)
    people = models.CharField(max_length = 300, null = True, blank = True)
    image = models.ImageField(upload_to = "user_image/%Y/%m/%d", max_length = 120, null = True, blank = True)
    fans = models.ManyToManyField('User', symmetrical = False, blank = True, null = True)
    new_at_count = models.IntegerField(default = 0)
    new_fans_count = models.IntegerField(default = 0)
    
    
    def __unicode__(self):
        return self.nick_name

class Message(models.Model):
    """Definition of MicroBlog Message between Users

    Necessary columns are:
        id --> auto increamental unique
        user_from --> foreignkey
        user_to --> foreignkey
        send_datetime --> datetime
        content --> limit 1000 characters
        read_flag --> integer from 0-1
        delete_flag --> integer from 0-3
    """

    user_from = models.ForeignKey('User', related_name = 'message_from')
    user_to = models.ForeignKey('User', related_name = 'message_to')
    send_datetime = models.DateTimeField(default = datetime.datetime.now)
    content = models.CharField(max_length = 1000)
    read_flag = models.IntegerField(choices = READ_CHOICES, default = READ_CHOICES[0][0])
    delete_flag = models.IntegerField(choices = DELETE_CHOICES, default = DELETE_CHOICES[0][0])

    def __unicode__(self):
        return  self.user_from.nick_name+u' To ' +self.user_to.nick_name

class MicroBlogPost(models.Model):
    """Definition of MicroBlog Post

    Necessary columns are:
        id --> auto increamental unique
        user --> foreignkey
        forward_count --> integer
        post_datetime --> datetime
        final_source --> foreignkey
        last_source --> foreignkey
        content --> limit 140 characters
        ats --> many to many field
    """

    user = models.ForeignKey('User', related_name = 'posts_blog')
    forward_count = models.IntegerField(default = 0)
    post_datetime = models.DateTimeField(default = datetime.datetime.now)
    final_source = models.ForeignKey('MicroBlogPost', related_name = 'branch_post', null = True, blank = True, on_delete = models.SET_NULL)
    last_source = models.ForeignKey('MicroBlogPost', related_name = 'son_post', null = True, blank = True, on_delete = models.SET_NULL)
    content = models.CharField(max_length = 10)
    ats = models.ManyToManyField('User', related_name = 'ats_blog', blank = True, null = True)

    def __unicode__(self):
        return self.content

class MicroBlogReply(models.Model):
    """Definition of MicroBlog Reply

    Necessary columns are:
        id --> auto increamental unique
        blog_post --> foreignkey
        post_datetime --> datetime
        content --> limit 140 characters
    """

    blog_post = models.ForeignKey('MicroBlogPost')
    post_datetime = models.DateTimeField(default = datetime.datetime.now)
    content = models.CharField(max_length = 140)

    def __unicode__(self):
        return self.content

    
    
admin.site.register(User)
admin.site.register(Message)
admin.site.register(MicroBlogPost)
admin.site.register(MicroBlogReply)


    
